package com.yang.jxj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.jxj.entity.Rules;
import com.yang.jxj.entity.Score;
import com.yang.jxj.entity.Students;
import com.yang.jxj.entity.Teachers;
import com.yang.jxj.service.impl.TeachersServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yang
 * @since 2024-06-22
 */
@RestController
@RequestMapping("/api/teachers")
public class TeachersController {

    @Autowired
    private TeachersServiceImpl teach;

    @GetMapping("/message")
    public ResponseEntity<HashMap<String,Object>> test(HttpServletRequest request) {
        HashMap<String,Object> map = new HashMap<>();
        Integer userId = Integer.valueOf(String.valueOf(request.getAttribute("userId"))) ;
        Teachers byId = teach.getById(userId);
        map.put("teacher", byId);

        return ResponseEntity.ok(map);
    }

    @PostMapping("/save")
    public HashMap<String,Object> save(@RequestBody Teachers teacher) {
        HashMap<String,Object> map = new HashMap<>();
        boolean b = teach.saveOrUpdate(teacher);
        if(b){
            map.put("code",0);
            map.put("msg","成功");
            return map;
        }else{
            map.put("code",1);
            map.put("msg","失败");
            return map;
        }
    }
}
